﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client.InfoSys
{
    public partial class PasswordModifyWnd : UserControl
    {
        #region 成员函数
        //private SysAccount _currentRecord;
        #endregion

        #region 构造函数
        public PasswordModifyWnd()
        {
            InitializeComponent();
            InitBtnImage();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.key;
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (this.tbOldPassword.Text.Trim() == "")
            {
                bRet = false;
                tbOldPassword.BackColor = Color.Yellow;
            }
            if (this.tbNewPassword.Text.Trim() == "")
            {
                bRet = false;
                tbNewPassword.BackColor = Color.Yellow;
            }
            if (this.tbNewPasswordCfm.Text.Trim() == "")
            {
                bRet = false;
                tbNewPasswordCfm.BackColor = Color.Yellow;
            }
            if (!bRet)
            {
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();

            //this._currentRecord = new SysAccount();
            //this._currentRecord.Id = MainForm.CurrentUser.Id;
            //this._currentRecord = client.QuerySysAccount(this._currentRecord);
            string strHashPassword = GetHashValue.Cryptography.GetHashValue(this.tbOldPassword.Text.Trim());
            if (strHashPassword.CompareTo(MainForm.CurrentUser.Password) != 0)
            {
                MessageBox.Show("原密码错误，请重新输入！");
                return;
            }
            if (this.tbNewPassword.Text.Trim().CompareTo(this.tbNewPasswordCfm.Text.Trim()) != 0)
            {
                MessageBox.Show("新密码两次输入不符，请重新输入！");
                return;
            }

            if (MessageBox.Show("您确定要更改密码吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                MainForm.CurrentUser.Password = GetHashValue.Cryptography.GetHashValue(this.tbNewPassword.Text.Trim());
                bRet = client.UpdateSysAccount(MainForm.CurrentUser);
                if (bRet)
                {
                    MessageBox.Show("密码修改成功!");
                }
                else
                {
                    MessageBox.Show("密码修改失败！");
                    return;
                }
            }
        }
        #endregion
    }
}
